package service

import (
	"github.com/gin-gonic/gin"
	"monment/pkg"
	"net/http"
	"path/filepath"
)

func QiNiuYun(c *gin.Context) {
	file, err := c.FormFile("image")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "接收图片失败",
		})
		return
	}
	if file.Size > 1024*1024*2 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "图片大小不能大于2MB",
		})
		return
	}
	ext := filepath.Ext(file.Filename)
	if ext != ".png" && ext != ".jpg" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "图片格式不正确",
		})
		return
	}
	yun, err := pkg.QiNiuYun(file)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "上传图片失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传图片成功",
		"data": gin.H{
			"fileUrl": yun,
		},
	})
	return
}
